package com.air.admin.controller.imp;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.air.admin.controller.IBasicAdminController;
import com.air.admin.mo.AdminComponent;
import com.air.admin.mo.AdminFunctionItem;
import com.air.admin.mo.AdminPrivilege;
import com.air.admin.mo.AdminRole;
import com.air.admin.mo.BasicAdminInfo;
import com.air.admin.service.IAdminComponentService;
import com.air.admin.service.IAdminFunctionItemService;
import com.air.admin.service.IBasicAdminInfoService;
import com.air.admin.vo.AdminLoginRecorderVO;
import com.air.common.controller.imp.BaseControllerImp;
import com.air.common.exception.NoSuchUserException;
import com.air.common.exception.WrongPasswordException;
import com.air.common.service.ISystemConfigService;
import com.air.common.util.BasicAppConstant;
import com.air.common.util.BasicErrorCode;
import com.air.common.util.WebUtil;
import com.air.common.vo.LoginRequestVO;
import com.air.common.vo.ResponseVO;

@Controller
@RequestMapping("/admin")
public class BasicAdminControllerImp extends BaseControllerImp implements
		IBasicAdminController {
	
	
	@Autowired
	ISystemConfigService systemConfigService;
	
	@Autowired
	IBasicAdminInfoService adminInfoService;
	
	@Autowired
	IAdminFunctionItemService adminFunctionItemService;
	
	@Autowired
	IAdminComponentService adminComponentService;
	
	@Override
	@RequestMapping("basic/preLogin")
	public ModelAndView preLogin(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String loginViewName = systemConfigService.findConfigByKey(BasicAppConstant._SYSCONF_KEY_ADMIN_LOGIN_PAGE).getValue();
		
		//ResponseVO responseVO = (ResponseVO) JSONObject.toBean((JSONObject)request.getAttribute(BasicAppConstant._KEY_RESPONSE_VO),ResponseVO.class);
		ResponseVO responseVO = new ResponseVO();
		responseVO.setViewName(loginViewName);
		return parse2Mav(request, responseVO);
	}
	
	@Override
	@RequestMapping("basic/login")
	public ModelAndView login(HttpServletRequest request,
			HttpServletResponse response, LoginRequestVO requestVO) throws Exception{
		// TODO Auto-generated method stub
		ResponseVO responseVO = new ResponseVO();
		String loginViewName = systemConfigService.findConfigByKey(BasicAppConstant._SYSCONF_KEY_ADMIN_LOGIN_PAGE).getValue();
		BasicAdminInfo adminInfo = null;
		try{
			adminInfo = adminInfoService.validateAdmin(requestVO.getLoginName().trim(), requestVO.getPassword());
		}catch(NoSuchUserException noUserEx){
			responseVO.setException(noUserEx);
			responseVO.setExecuteResult(false);
			responseVO.setErrCode(BasicErrorCode._ERRCODE_NO_SUCH_USER);
			responseVO.setViewName(loginViewName);
			responseVO.addObject(BasicAppConstant._KEY_LOGIN_RETURN_URL, requestVO.getReturnUrl());
			return parse2Mav(request, responseVO);
		}catch(WrongPasswordException wrongPwdEx){
			responseVO.setException(wrongPwdEx);
			responseVO.setExecuteResult(false);
			responseVO.setErrCode(BasicErrorCode._ERRCODE_WRONG_PASSWORD);
			responseVO.setViewName(loginViewName);
			responseVO.addObject(BasicAppConstant._KEY_LOGIN_RETURN_URL, requestVO.getReturnUrl());
			return parse2Mav(request, responseVO);
		} 
		
		
		List<AdminRole>      roles = adminInfoService.getRoles(adminInfo);
		List<AdminPrivilege> privileges = adminInfoService.getPrivileges(adminInfo);
		
		WebUtil.storeAdminLoginRecorderInSession(request, adminInfo,roles,privileges);
		
		
		responseVO.setViewName("redirect:/admin/index");
		return  parse2Mav(request, responseVO);
		
	}

	@Override
	@RequestMapping("logout")
	public ModelAndView logout(HttpServletRequest request,
			HttpServletResponse response) {
		
		WebUtil.removeAdminLoginRecorderInSession(request);
		ResponseVO responseVO=  new ResponseVO();
		responseVO.setViewName("redirect:/admin/index");
		return parse2Mav(request, responseVO);
	}

	

	@Override
	@RequestMapping("index")
	public ModelAndView toIndexView(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		
		AdminLoginRecorderVO loginRecorderVO=(AdminLoginRecorderVO) request.getSession().getAttribute(BasicAppConstant._KEY_ADMIN_SESSION);
		
		BasicAdminInfo adminInfo = loginRecorderVO.getAdminInfo();
		List<AdminRole>      roles = loginRecorderVO.getRoles();
		List<AdminPrivilege> privileges = loginRecorderVO.getPrivileges();
		//List<AdminRole>      roles = adminInfoService.getRoles(adminInfo);
		//List<AdminPrivilege> privileges = adminInfoService.getPrivileges(adminInfo);
		List<AdminFunctionItem> funItems =  adminFunctionItemService.getAvailableFunItems(privileges);
		
		ResponseVO responseVO = new ResponseVO();
		responseVO.setViewName("admin/index");
		responseVO.addObject("adminInfo", adminInfo);
		responseVO.addObject("roles", roles);
		responseVO.addObject("privileges", privileges);
		responseVO.addObject("funItems", funItems);
		
		return parse2Mav(request, responseVO);
	}

	@Override
	@RequestMapping(value="loadComponent/{componentCode}")
	public ModelAndView loadComponent(HttpServletRequest request,
			HttpServletResponse response, @PathVariable String componentCode)
			throws Exception {
		
		AdminLoginRecorderVO loginRecorderVO=(AdminLoginRecorderVO) request.getSession().getAttribute(BasicAppConstant._KEY_ADMIN_SESSION);
		AdminComponent component =  adminComponentService.findComponentByCode(componentCode);
		
		
		List<AdminPrivilege> privileges = loginRecorderVO.getPrivileges();
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName(BasicAppConstant._VIEW_ADMIN_COMPONENT);
		mav.addObject("component", component);
		mav.addObject("privileges",privileges);
		
		ResponseVO responseVO = new ResponseVO();
		responseVO.addObject("component", component);
		responseVO.addObject("privileges",privileges);
		responseVO.setViewName(BasicAppConstant._VIEW_ADMIN_COMPONENT);
		
		
		new JSONObject();
		
		
		return parse2Mav(request,responseVO);
	}
	
	
	
	
}
